import tkinter as tk
from tkinter import filedialog,messagebox
from tkinter import *

from PIL import Image,ImageTk
import od_cls_kernel9721 as knl


welcome_img="/home/cumtzd/car_od_cls/welcome.jpg"
od_output_img="/home/cumtzd/car_od_cls/od_output.jpg"
cls_output_img="/home/cumtzd/car_od_cls/cls_output.jpg"
filename2=''


root = tk.Tk()
root.title('Car detect&classify sys')
#filename2=StringVar()

def open_img_todo():
    global filename2
    filename2= filedialog.askopenfilename(filetypes=[("JPG",".jpg"),("JPEG",".jpeg")],initialdir="/home/cumtzd/od_test_img",title="Open image with jpg format")
    #filename2.set(fn_tmp)
    print(filename2)
    img=ImageTk.PhotoImage(file=filename2)
    label_one.config(image=img)
    label_one.image=img
    
       
def object_detection():
    print("object_detecting........")
    global filename2
    num_of_car=knl.object_detect_classify(filename2)
    if num_of_car==0:
        messagebox.showwarning(title='warning',message="Warning:can't detect score >0.5 of Car")
    print("detect %d cars"%num_of_car)
    
    img=ImageTk.PhotoImage(file=od_output_img)
    label_one.config(image=img)
    label_one.image=img
    
    
def car_classify():
    print("car_classifying........")
    print(filename2+"classify")
    
    img=ImageTk.PhotoImage(file=cls_output_img)
    label_one.config(image=img)
    label_one.image=img
    

img=ImageTk.PhotoImage(file=welcome_img)
label_one=tk.Label(root,image=img)
label_one.grid(row=0,columnspan=3,padx=3,pady=5)
    
b1=Button(root,text="Open-img",command=open_img_todo)
b1.grid(row=1,column=0,pady=5)
b1=Button(root,text="Object-Detect",command=object_detection)
b1.grid(row=1,column=1,pady=5)
b1=Button(root,text="Classify",command=car_classify)
b1.grid(row=1,column=2,pady=5)


mainloop()
